import { Component, OnInit } from '@angular/core';
import * as moment from 'moment';
import * as _ from 'lodash';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { FetchService } from '../../../core/net/fetch.service';
import { differenceInCalendarDays } from 'date-fns';

@Component({
    selector: 'app-year-plan',
    templateUrl: './year-plan.component.html',
    styleUrls: ['./year-plan.component.scss']
})
export class YearPlanComponent implements OnInit {
    year = new Date();
    tabIndex = 0;

    /*************  年计划审核  *************/
    sectionId;
    approveTableData = [];
    tableHeight = '400px';
    scrollConfig = {
        y: '400px'
    };
    pageNum = 1;
    pageSize = 10;
    total: number;
    loading = true;

    /*************  年计划上报  *************/
    reportTableData = [];

    constructor(private message: NzMessageService,
                private fetchService: FetchService) {
    }

    ngOnInit(): void {
        this.getGridData();
    }

    disabledDate = (current: Date): boolean => {
        // 不能选明年之后的日期
        return differenceInCalendarDays(current, moment().add(1, 'y').valueOf()) > 0;
    };

    getGridData(): void {
        if (this.tabIndex === 0) {
            this.getApproveData();
        } else {
            this.getReportData();
        }
    }

    tabOnChange(): void {

    }

    getApproveData(): void {
        const params = {
            sectionId: !this.sectionId || this.sectionId === '1' ? '' : this.sectionId,
            y: moment(this.year).year() || null
        };
        this.fetchService.post<any>('sys/yjqYearPlan/queryList', params).subscribe(res => {
            this.loading = false;
            if (res && Array.isArray(res)) {
                this.total = res.length;
                this.approveTableData = res;
            } else {
                this.total = 0;
                this.approveTableData = [];
            }
        });
    }

    reset(): void {
        this.year = null;
        this.sectionId = null;
        this.getApproveData();
    }

    openDetailPage(type, name?, sectionId?, year?): void {
        // this.router.navigate(['/dispatch-plan/sub-year-plan-add'], { skipLocationChange: true, queryParams: {type: type, name: name, year: year, sectionId: sectionId}});
    }

    getReportData(): void {

    }
}
